package ynu.edu.order.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import ynu.edu.common.domain.PageDTO;
import ynu.edu.common.domain.R;
import ynu.edu.common.utils.BeanUtils;
import ynu.edu.order.domain.dto.OrderFormDTO;
import ynu.edu.order.domain.vo.OrderDetailVO;
import ynu.edu.order.domain.vo.OrderVO;
import ynu.edu.order.service.IOrderService;

import java.util.List;

@Api(tags = "订单管理接口")
@RestController
@RequestMapping("/orders")
@RequiredArgsConstructor
@Slf4j
public class OrderController {
    private final IOrderService orderService;

    @ApiOperation("根据id查询订单")
    @GetMapping("/{id}")
    public R<OrderVO> queryOrderById(@PathVariable("id") Long orderId) {
        try {
            OrderVO orderVO = BeanUtils.copyBean(orderService.getById(orderId), OrderVO.class);
            if (orderVO == null) {
                return R.error("订单不存在");
            }
            return R.ok(orderVO);
        } catch (Exception e) {
            log.error("查询订单失败，订单ID: {}", orderId, e);
            return R.error("查询订单失败");
        }
    }

    @ApiOperation("查询订单详情")
    @GetMapping("/{id}/details")
    public R<List<OrderDetailVO>> queryOrderDetails(@PathVariable("id") Long orderId) {
        try {
            List<OrderDetailVO> details = orderService.queryOrderDetails(orderId);
            return R.ok(details);
        } catch (Exception e) {
            log.error("查询订单详情失败，订单ID: {}", orderId, e);
            return R.error("查询订单详情失败");
        }
    }

    @ApiOperation("分页查询用户订单")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "page", value = "页码", paramType = "query", defaultValue = "1"),
        @ApiImplicitParam(name = "size", value = "每页大小", paramType = "query", defaultValue = "10"),
        @ApiImplicitParam(name = "status", value = "订单状态", paramType = "query", required = false)
    })
    @GetMapping
    public R<PageDTO<OrderVO>> queryUserOrders(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) Integer status) {
        try {
            PageDTO<OrderVO> orderPage = orderService.queryUserOrders(page, size, status);
            return R.ok(orderPage);
        } catch (Exception e) {
            log.error("查询用户订单失败", e);
            return R.error("查询用户订单失败");
        }
    }

    @ApiOperation("查询所有订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query", defaultValue = "1"),
            @ApiImplicitParam(name = "size", value = "每页大小", paramType = "query", defaultValue = "10")
    })
    @GetMapping("/all")
    public R<PageDTO<OrderVO>> queryAllOrders(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size){
        try {
            PageDTO<OrderVO> orderPage = orderService.queryAllOrders(page, size);
            return R.ok(orderPage);
        } catch (Exception e) {
            log.error("查询所有订单失败", e);
            return R.error("查询所有订单失败");
        }
    }

    @ApiOperation("创建订单")
    @PostMapping
    public R<Long> createOrder(@RequestBody OrderFormDTO orderFormDTO) {
        try {
            Long orderId = orderService.createOrder(orderFormDTO);
            return R.ok(orderId);
        } catch (Exception e) {
            log.error("创建订单失败", e);
            return R.error("创建订单失败: " + e.getMessage());
        }
    }

    @ApiOperation("取消订单")
    @ApiImplicitParam(name = "orderId", value = "订单id", paramType = "path")
    @DeleteMapping("/{orderId}")
    public R<Void> cancelOrder(@PathVariable("orderId") Long orderId) {
        try {
            orderService.cancelOrder(orderId);
            return R.ok();
        } catch (Exception e) {
            log.error("取消订单失败，订单ID: {}", orderId, e);
            return R.error("取消订单失败: " + e.getMessage());
        }
    }

    @ApiOperation("标记订单已支付")
    @ApiImplicitParam(name = "orderId", value = "订单id", paramType = "path")
    @PutMapping("/{orderId}/pay")
    public R<Void> markOrderPaySuccess(@PathVariable("orderId") Long orderId) {
        try {
            orderService.markOrderPaySuccess(orderId);
            return R.ok();
        } catch (Exception e) {
            log.error("标记订单支付失败，订单ID: {}", orderId, e);
            return R.error("标记订单支付失败: " + e.getMessage());
        }
    }

    @ApiOperation("确认收货")
    @ApiImplicitParam(name = "orderId", value = "订单id", paramType = "path")
    @PutMapping("/{orderId}/confirm")
    public R<Void> confirmOrder(@PathVariable("orderId") Long orderId) {
        try {
            orderService.confirmOrder(orderId);
            return R.ok();
        } catch (Exception e) {
            log.error("确认收货失败，订单ID: {}", orderId, e);
            return R.error("确认收货失败: " + e.getMessage());
        }
    }

    @ApiOperation("申请退款")
    @ApiImplicitParam(name = "orderId", value = "订单id", paramType = "path")
    @PutMapping("/{orderId}/refund")
    public R<Void> applyRefund(@PathVariable("orderId") Long orderId) {
        try {
            orderService.applyRefund(orderId);
            return R.ok();
        } catch (Exception e) {
            log.error("申请退款失败，订单ID: {}", orderId, e);
            return R.error("申请退款失败: " + e.getMessage());
        }
    }
}
